package com.chinawlx.wlxteacher.utils;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;

import java.util.Stack;

/**
 * 作者:吴名
 * 时间:16/8/10
 * 描述:管理Activity的工具类
 */
public class WLXActivityManagerUtil {
    private static Stack<Activity> mActivityStack = new Stack<>();

    /**
     * 吴名16/1/13
     * 添加Activity到堆栈
     */
    public static void addActivity(Activity aty) {
        if (aty != null) {
            mActivityStack.add(aty);
        }
    }


    /**
     * 结束指定类名的Activity
     */
    public static void finishActivity(Class<?> cls) {
        for (Activity activity : mActivityStack) {
            if (activity.getClass().equals(cls)) {
                activity.finish();
                break;
            }
        }
    }

    /**
     * 结束所有Activity
     */
    public static void finishAllActivity() {

        for (Activity activity : mActivityStack) {
            if (activity != null) {
                activity.finish();
            }
        }
        mActivityStack.clear();
    }

    /**
     * 退出应用程序
     */
    public static void appExit(Context context) {
        try {
            finishAllActivity();
            ActivityManager activityMgr = (ActivityManager) context
                    .getSystemService(Context.ACTIVITY_SERVICE);
            activityMgr.killBackgroundProcesses(context.getPackageName());
            System.exit(0);
        } catch (Exception e) {
        }
    }
}
